package es.udc.emarketapp.http.view.actionForms;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import es.udc.fbellas.j2ee.util.struts.action.PropertyValidator;
import es.udc.fbellas.j2ee.util.struts.action.DefaultActionForm;
import org.apache.struts.upload.FormFile;

public class ProductForm extends DefaultActionForm {

    private Long idProd;
    private Long idCat;
    private String name;
    private String description;
    private String price;
    private double priceAsDouble;
    private String weight;
    private double weightAsDouble;
    private String uds;
    private Long udsAsLong;
    private String country;
    private FormFile image;

    public ProductForm() {
        reset();
    }

    public Long getIdProd() {
        return idProd;
    }

    public void setIdProd(Long idProd) {
        this.idProd = idProd;
    }

    public FormFile getImage() {
        return image;
    }

    public void setImage(FormFile image) {
        this.image = image;
    }

    public Long getIdCat() {
        return idCat;
    }

    public void setIdCat(Long idCat) {
        this.idCat = idCat;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name.trim();
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description.trim();
    }

    public String getPrice() {
        return price;
    }

    public double getPriceAsDouble() {
        return priceAsDouble;
    }

    public void setPriceAsDouble(Double priceAsDouble) {
        this.priceAsDouble = priceAsDouble;
    }

    public void setPrice(String price) {
        this.price = price.trim();
    }

    public String getWeight() {
        return weight;
    }

    public double getWeightAsDouble() {
        return weightAsDouble;
    }

    public void setWeightAsDouble(Double weightAsDouble) {
        this.weightAsDouble = weightAsDouble;
    }

    public void setWeight(String weight) {
        this.weight = weight.trim();
    }

    public void setUds(String uds) {
        this.uds = uds.trim();
    }

    public String getUds() {
        return uds;
    }

    public Long getUdsAsLong() {
        return udsAsLong;
    }

    public void setUdsAsLong(Long udsAsLong) {
        this.udsAsLong = udsAsLong;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();
        PropertyValidator.validateMandatory(errors, "name", name);
        PropertyValidator.validateMandatory(errors, "description", description);
        priceAsDouble = PropertyValidator.validateDouble(errors, "price",
                price, true, 0.01, 10000, getLocale(request));
        weightAsDouble = PropertyValidator.validateDouble(errors, "weight",
                weight, true, 0.01, 200, getLocale(request));
        udsAsLong = PropertyValidator.validateLong(errors, "uds", uds, true, 1,
                Long.MAX_VALUE);

        return errors;

    }

    private void reset() {
        idProd = null;
        name = null;
        idCat = null;
        name = null;
        description = null;
        price = null;
        priceAsDouble = 0;
        weight = null;
        weightAsDouble = 0;
        uds = null;
        udsAsLong = null;
        country = null;
        image = null;

    }
}
